package com.arcvideo.acsplayer.widget;

import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.arcvideo.acsplayer.R;

public class HDselectView extends FrameLayout {

	public static final String STATE_4K = "4k";
	public static final String STATE_ULTRA_CLEAR = "超清";
	public static final String STATE_HD = "高清";
	public static final String STATE_SD = "标清";
	public static final String STATE_SMOOTH = "流畅";

	private Context mContext = null;
	private LinearLayout mLandLayout = null;
	private RelativeLayout mPortLayout = null;
	private ImageButton mBtn4k_port = null;
	private ImageButton mBtnUltraClear_port = null;
	private ImageButton mBtnHD_port = null;
	private ImageButton mBtnSD_port = null;
	private ImageButton mBtnSmooth_port = null;

	private ImageButton mBtn4k_land = null;
	private ImageButton mBtnUltraClear_land = null;
	private ImageButton mBtnHD_land = null;
	private ImageButton mBtnSD_land = null;
	private ImageButton mBtnSmooth_land = null;

	private String mCurrentState = STATE_HD;
	private int mRotationType = 0;
	private SelectInterface mSelectInterface = null;

	/**
	 * 回调接口
	 *
	 * @author skl5250
	 *
	 */
	public interface SelectInterface {
		public void setState(String state);
	}

	/**
	 * 按键事件
	 */
	private OnClickListener mOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View view) {
			switch (view.getId()) {
				case R.id.btn_4k_port :
				case R.id.btn_4k_land :
					mCurrentState = STATE_4K;
					break;
				case R.id.btn_ultra_clear_port :
				case R.id.btn_ultra_clear_land :
					mCurrentState = STATE_ULTRA_CLEAR;
					break;
				case R.id.btn_hd_port :
				case R.id.btn_hd_land :
					mCurrentState = STATE_HD;
					break;
				case R.id.btn_sd_port :
				case R.id.btn_sd_land :
					mCurrentState = STATE_SD;
					break;
				case R.id.btn_smooth_port :
				case R.id.btn_smooth_land :
					mCurrentState = STATE_SMOOTH;
					break;
				default :
					mCurrentState = STATE_HD;
					break;
			}
			setState(mCurrentState);
			mSelectInterface.setState(mCurrentState);
			setVisibility(View.GONE);
		}
	};

	/**
	 * 构造函数
	 *
	 * @param context
	 */
	public HDselectView(Context context) {
		super(context);
		mContext = context;
	}

	/**
	 * 构造函数
	 *
	 * @param context
	 * @param attributeSet
	 */
	public HDselectView(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		mContext = context;
	}

	/**
	 * 初始化
	 *
	 * @param rotationType
	 * @param state
	 * @param selectInterface
	 */
	public void init(int rotationType, String state, SelectInterface selectInterface) {
		mRotationType = rotationType;
		mCurrentState = state;
		mSelectInterface = selectInterface;
		LayoutInflater.from(mContext).inflate(R.layout.actionbar_hd_layout, this);
		// if (rotationType == Configuration.ORIENTATION_LANDSCAPE) {
		// LayoutInflater.from(mContext).inflate(R.layout.actionbar_hd_layout_land,
		// this);
		// } else if (rotationType == Configuration.ORIENTATION_PORTRAIT) {
		// LayoutInflater.from(mContext).inflate(R.layout.actionbar_hd_layout_port,
		// this);
		// }
		initUI();
		setState(mCurrentState);
	}

	/**
	 * 初始化UI
	 */
	public void initUI() {
		mPortLayout = (RelativeLayout) findViewById(R.id.hd_port_layout);
		mBtn4k_port = (ImageButton) findViewById(R.id.btn_4k_port);
		mBtn4k_port.setOnClickListener(mOnClickListener);
		mBtnUltraClear_port = (ImageButton) findViewById(R.id.btn_ultra_clear_port);
		mBtnUltraClear_port.setOnClickListener(mOnClickListener);
		mBtnHD_port = (ImageButton) findViewById(R.id.btn_hd_port);
		mBtnHD_port.setOnClickListener(mOnClickListener);
		mBtnSD_port = (ImageButton) findViewById(R.id.btn_sd_port);
		mBtnSD_port.setOnClickListener(mOnClickListener);
		mBtnSmooth_port = (ImageButton) findViewById(R.id.btn_smooth_port);
		mBtnSmooth_port.setOnClickListener(mOnClickListener);

		mLandLayout = (LinearLayout) findViewById(R.id.hd_land_layout);
		mBtn4k_land = (ImageButton) findViewById(R.id.btn_4k_land);
		mBtn4k_land.setOnClickListener(mOnClickListener);
		mBtnUltraClear_land = (ImageButton) findViewById(R.id.btn_ultra_clear_land);
		mBtnUltraClear_land.setOnClickListener(mOnClickListener);
		mBtnHD_land = (ImageButton) findViewById(R.id.btn_hd_land);
		mBtnHD_land.setOnClickListener(mOnClickListener);
		mBtnSD_land = (ImageButton) findViewById(R.id.btn_sd_land);
		mBtnSD_land.setOnClickListener(mOnClickListener);
		mBtnSmooth_land = (ImageButton) findViewById(R.id.btn_smooth_land);
		mBtnSmooth_land.setOnClickListener(mOnClickListener);
		reload(mRotationType);
	}

	/**
	 * 重新加載
	 *
	 * @param rotationType
	 */
	public void reload(int rotationType) {
		mRotationType = rotationType;
		if (rotationType == Configuration.ORIENTATION_LANDSCAPE) {
			mPortLayout.setVisibility(GONE);
			mLandLayout.setVisibility(VISIBLE);
		} else if (rotationType == Configuration.ORIENTATION_PORTRAIT) {
			mPortLayout.setVisibility(VISIBLE);
			mLandLayout.setVisibility(GONE);
		}
	}

	/**
	 * 设置画质状态
	 *
	 * @param currentState
	 */
	public void setState(String currentState) {
		mCurrentState = currentState;
		ImageButton btn4k = null;
		ImageButton btnUltraClear = null;
		ImageButton btnHD = null;
		ImageButton btnSD = null;
		ImageButton btnSmooth = null;

		if (mRotationType == Configuration.ORIENTATION_LANDSCAPE) {
			btn4k = mBtn4k_land;
			btnUltraClear = mBtnUltraClear_land;
			btnHD = mBtnHD_land;
			btnSD = mBtnSD_land;
			btnSmooth = mBtnSmooth_land;
		} else if (mRotationType == Configuration.ORIENTATION_PORTRAIT) {
			btn4k = mBtn4k_port;
			btnUltraClear = mBtnUltraClear_port;
			btnHD = mBtnHD_port;
			btnSD = mBtnSD_port;
			btnSmooth = mBtnSmooth_port;
		}

		if (mCurrentState.equals(STATE_4K)) {
			btn4k.setBackgroundResource(R.drawable.btn_4k_p);
			btnUltraClear.setBackgroundResource(R.drawable.btn_ultra_clear);
			btnHD.setBackgroundResource(R.drawable.btn_hd);
			btnSD.setBackgroundResource(R.drawable.btn_sd);
			btnSmooth.setBackgroundResource(R.drawable.btn_smooth);
		} else if (mCurrentState.equals(STATE_ULTRA_CLEAR)) {
			btn4k.setBackgroundResource(R.drawable.btn_4k);
			btnUltraClear.setBackgroundResource(R.drawable.btn_ultra_clear_p);
			btnHD.setBackgroundResource(R.drawable.btn_hd);
			btnSD.setBackgroundResource(R.drawable.btn_sd);
			btnSmooth.setBackgroundResource(R.drawable.btn_smooth);
		} else if (mCurrentState.equals(STATE_HD)) {

			btn4k.setBackgroundResource(R.drawable.btn_4k);
			btnUltraClear.setBackgroundResource(R.drawable.btn_ultra_clear);
			btnHD.setBackgroundResource(R.drawable.btn_hd_p);
			btnSD.setBackgroundResource(R.drawable.btn_sd);
			btnSmooth.setBackgroundResource(R.drawable.btn_smooth);
		} else if (mCurrentState.equals(STATE_SD)) {
			btn4k.setBackgroundResource(R.drawable.btn_4k);
			btnUltraClear.setBackgroundResource(R.drawable.btn_ultra_clear);
			btnHD.setBackgroundResource(R.drawable.btn_hd);
			btnSD.setBackgroundResource(R.drawable.btn_sd_p);
			btnSmooth.setBackgroundResource(R.drawable.btn_smooth);
		} else if (mCurrentState.equals(STATE_SMOOTH)) {
			btn4k.setBackgroundResource(R.drawable.btn_4k);
			btnUltraClear.setBackgroundResource(R.drawable.btn_ultra_clear);
			btnHD.setBackgroundResource(R.drawable.btn_hd);
			btnSD.setBackgroundResource(R.drawable.btn_sd);
			btnSmooth.setBackgroundResource(R.drawable.btn_smooth_p);
		}
	}

	/**
	 * 显示
	 */
	public void show() {
		setVisibility(View.VISIBLE);
	}

	/**
	 * 隱藏
	 */
	public void hide() {
		setVisibility(View.GONE);
	}

}